package org.dwell.vertx3.demos.http;

import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;

/**
 * Created by JianGuo on 2016/1/15.
 */
public class HttpServerTest {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        HttpServerOptions options = new HttpServerOptions().setPort(9999);
        HttpServer server = vertx.createHttpServer(options);

        //设置对客户端请求的响应处理
        server.requestHandler(res -> {
            //Handle the request in here
            //http://localhost:9999/some-uri
            System.out.println(res.absoluteURI());

            //GET、POST、PUT、DELETE、HEAD、OPTIONS etc
            System.out.println(res.method());

            //params()返回的结果是一个multiMap
            //比如，请求的地址是：a/b/c/page.html?param1=abc&param2=xyz
            //那么params()的结果是
            //param1: abc
            //param2: xyz
            System.out.println(res.params());

            //比如，请求的地址是：a/b/c/page.html?param1=abc&param2=xyz
            //那么path()的结果是：/a/b/c/page.html
            System.out.println(res.path());

            //比如，请求的地址是：a/b/c/page.html?param1=abc&param2=xyz
            //那么query()的结果是：param1=abc&param2=xyz
            System.out.println(res.query());

            //获取请求header信息
            System.out.println(res.headers());
            MultiMap headers = res.headers();
            System.out.println(headers.get("user-agent"));
            System.out.println(headers.get("User-Agent"));

            //当请求的所有内容都被读取完毕，这个方法会被调用
            res.endHandler(h -> {
                System.out.println("end handler");
            });


            //Reading data from the request body
            //处理客户端的请求数据
            res.handler(buffer -> {
                System.out.println("I have received a chunk of body of length " + buffer.length());
            });

        });



        server.listen();




    }

}
